<?php

namespace app\api\controller;

use app\admin\model\teacher\Share;
use app\common\controller\Api;
use think\Db;

class Activitys extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function indexActivity()
    {
        $token = $this->auth->getToken();
        $uid = $this->getToken($token);
        //个人头像昵称
        $nickname = Db::table('fa_user')->where('id', $uid)->field('nickname,avatar,jointime,pid')->find();
        $parentInfo = Db::table('fa_user')->where('id', $nickname['pid'])->field('id,nickname,avatar')
            ->find();
        $fansList = Db::table('fa_user')->where('pid', $uid)->field('id,nickname,avatar,jointime')->select()->toArray();
        $firstCount = Share::where('user_id', $uid)->field('id,first_children,active_ge')->find();
        foreach ($fansList as $key => $value) {
            $fansActive[] = Share::where('user_id', $value['id'])->field('user_id,active_ge')->find();
            $fansList[$key]['jointime'] = date('Y-m-d H:i:s', $value['jointime']);
        }
        foreach ($fansList as &$fan) {
            // 查找与当前粉丝ID匹配的活动记录
            $matchingActive = null;
            foreach ($fansActive as $kk => $active) {
                if (!is_null($active) && $active['user_id'] === $fan['id']) {
                    $matchingActive = $active;
                    break; // 找到匹配项后立即跳出循环
                }
            }
            // 如果找到匹配的活动记录，则将其 active_zong 值赋予粉丝数据
            if (!is_null($matchingActive)) {
                $fan['active_ge'] = $matchingActive['active_ge'];
            } else {
                // 或者设置一个默认值或空值
                $fan['active_ge'] = 0;
            }
        }
        $this->success('', ['user' => $nickname,
            'userActive' => $firstCount,
            'parentInfo' => $parentInfo,
            'list' => $fansList]);
    }
}